<?php
class Lottery_prize_model extends CI_Model
{
    private $_table   = 'lottery_prize';
    private $_table1  = 'prize_every_day';

      /**
     * 查询某抽奖活动的所有奖品
     * @param unknown $lottery_id
     */
    public function findPrizes($params)
    {
        $this->db->select('lottery_id, lottery_prize.prize_id, type, prize_name,grade,image,quantity,surplus_quantity,probability,effective_days, links, amount, winners_user, time_slot');
        $this->db->from($this->_table.' AS lottery_prize');
        $this->db->join($this->_table1.' AS prize_every_day', 'lottery_prize.prize_id=prize_every_day.prize_id', 'INNER');
        $this->db->where('lottery_id', $params['lottery_id']);
        $this->db->where('prize_every_day.date', date('Y-m-d'));
        $this->db->where('lottery_prize.audit_state', 2);//审核通过
        $this->db->where('lottery_prize.status', 1);//上架
        $this->db->where('prize_every_day.status', 1);//上架
        $this->db->group_by('lottery_prize.prize_id');
        $this->db->order_by('grade', 'ASC');
        return $this->db->get();
    }

    /**
     * 查询某抽奖活动的今日剩余奖品总数
     * @param unknown $lottery_id
     */
    public function findTotalTodayNums($lottery_id)
    {
		$this->db->select('sum(`surplus_quantity`) as totalNumber');
        $this->db->from($this->_table.' AS lottery_prize');
        $this->db->join($this->_table1.' AS prize_every_day', 'lottery_prize.prize_id=prize_every_day.prize_id', 'INNER');
        $this->db->where('lottery_id', $lottery_id);
        $this->db->where('prize_every_day.date', date('Y-m-d'));
        $this->db->where('lottery_prize.status', 1);//上架
        $this->db->where('prize_every_day.status', 1);//上架
        $this->db->where('lottery_prize.flag', 0);//常规产品
		$result = $this->db->get();
		if ($result->num_rows() > 0) {
			return $result->row()->totalNumber;
		}
        return 0;
    }
}